﻿@model LeadMaster.ViewModels.ViewModelOrders
@using LeadMaster.Models;

@using (Html.BeginForm("Edit","Orders")) 
{
    @Html.ValidationSummary(true)

    <fieldset>
    <legend>Edit Order</legend>
    <div id="accordion">       
        <h3>Main Details</h3>
        <div>
        <div class="editor-label" style="width: 100%">
            @Html.LabelFor(model => model.Order.Title)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Order.Title, new { @class = "largetextbox" })
            @Html.ValidationMessageFor(model => model.Order.Title)
        </div>
        <div class="spacer"></div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Order.CompanyID, "Outsourcer Company")
        </div>
        <div class="editor-field">
            @Html.DropDownList("CompanyID", null, new { style = "width:100%" })
            @Html.ValidationMessageFor(model => model.Order.CompanyID)
        </div>
        <div class="spacer"></div>

        <div class="editor-label" style="width: 100%">
            @Html.LabelFor(model => model.Order.FrameworkAgreementID, "Framework Agreement")
        </div>
        <div class="editor-field">
            @Html.DropDownList("FrameworkAgreementID", null, new { style = "width:100%" })
            @Html.ValidationMessageFor(model => model.Order.FrameworkAgreement.FrameworkAgreementID)
        </div>
        <div class="spacer"></div>
		
        <div class="editor-label">
            @Html.LabelFor(model => model.Order.LeadsReplacementID)
        </div>
        <div class="editor-field">
            @Html.DropDownList("Order_LeadsReplacementID", null, new { style = "width:50%" })
            @Html.ValidationMessageFor(model => model.Order.LeadsReplacementID)
        </div>
        
        <div style="width: 200px; float: left">
        <div class="editor-label">
            @Html.LabelFor(model => model.Order.InvoicingID, "Invoicing")
        </div>
        <div class="editor-field">
            @Html.DropDownList("InvoicingID", String.Empty)
            @Html.ValidationMessageFor(model => model.Order.InvoicingID)
        </div>
        </div>

        <div style="width: 200px; float: left">
        <div class="editor-label">
            @Html.LabelFor(model => model.Order.PaymentTermsID, "Payment Terms")
        </div>
        <div class="editor-field">
            @Html.DropDownList("PaymentTermsID", String.Empty)
            @Html.ValidationMessageFor(model => model.Order.PaymentTermsID)
        </div>
        </div>
        
        <div style="width: 200px; float: left">
        <div class="editor-label">
            @Html.LabelFor(model => model.Order.Guarantee, "Guarantee")
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Order.Guarantee, new { @class="notes"})
            @Html.ValidationMessageFor(model => model.Order.Guarantee)
        </div>
        </div>

        <div style="clear: both">
            @Html.LabelFor(model => model.Order.OrderStatusID, "Order Status")
        </div>
        <div class="editor-field">
            @Html.DropDownList("OrderStatusID", String.Empty)
            @Html.ValidationMessageFor(model => model.Order.OrderStatusID)
        </div>
        </div>
        <h3>Template Sections</h3>
        <div style="height:600px; overflow: scroll;">
            <div class="editor-label" style="width: 100%">
                @Html.LabelFor(model => model.Order.ScheduleTemplateID, "Template")
            </div>
            <div class="editor-field">
                @Html.DropDownList("ScheduleTemplateID", null, new { style = "width:100%" })
                @Html.ValidationMessageFor(model => model.Order.ScheduleTemplateID)
            </div>
            <div class="spacer"></div>
            <label class="smalllabel">In the following section, please enter the content that will replace the merge field in the mandatory sections listed.</label>
            <div class="spacer"></div>
            <div id="templatesection">
            @{
               int iCounter = 0;
            }
            @foreach (OrderSections orderSection in Model.OrderSections)
            {
                @Html.HiddenFor(model => model.OrderSections[iCounter].ScheduleSectionsID)
                @Html.HiddenFor(model => model.OrderSections[iCounter].OrderID)
                @Html.HiddenFor(model => model.OrderSections[iCounter].OrderSectionsID)
                <div class="editor-field">
                    @Html.TextAreaFor(model => model.OrderSections[iCounter].ReplacementHeader, new { @class = "largetextbox" })
                    <br />
                    <div style="width: 700px;">
                        @Html.TextAreaFor(model => model.OrderSections[iCounter].Content, new { @class = "sections" })
                    </div>
                </div>
                <div class="spacer"></div>
                iCounter++;
            }

            </div>
        </div>
        <h3>Fees</h3>
        <div>
        <label class="smalllabel" id="feesstart">Fees. Please enter the content for 'Other', and then the products.</label>
        <div class="editor-label">
            @Html.Label("Fees Other")
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Order.FeesOther, new { @class = "sections" })
            @Html.ValidationMessageFor(model => model.Order.FeesOther)
        </div>
        <div class="spacer"></div>
        @{
        Model.FeeCounter=0;
        foreach (Fees fee in Model.TheFees)
        {
            @Html.Partial("Fee")            
            Model.FeeCounter++;
        }
        }
        <div id="newfeehere"></div>
        <input type="button" value="Add fee" id="buttonAddFee" />
        </div>
    </div>
    @Html.HiddenFor(model => model.Order.OrderID)

    </fieldset>
        <div id="formSubmit">
        <div style="margin-top: 10px; float: right;">
            <input type="submit" value="Save" class="bigsubmit"/>
            <input type="button" value="Close" class="bigsubmit" id="ordereditcancel" />
        </div>
    </div>
}
<input type="hidden" value="@(Model.FeeCounter)" id="feeCounter" />


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}


<script type="text/javascript">
    $(document).ready(function () {

        $("#Order_LeadsReplacementID").attr('name', 'Order.LeadsReplacementID');
        $("#InvoicingID").attr('name', 'Order.InvoicingID');
        $("#PaymentTermsID").attr('name', 'Order.PaymentTermsID');
        $("#ScheduleTemplateID").attr('name', 'Order.ScheduleTemplateID');
        $("#FrameworkAgreementID").attr('name', 'Order.FrameworkAgreementID');
        $("#CompanyID").attr('name', 'Order.CompanyID');
        $("#OrderStatusID").attr('name', 'Order.OrderStatusID');
        $("#Order_LeadsReplacementID").val("@(Model.Order.LeadsReplacementID)");
        $("#InvoicingID").val("@(Model.Order.InvoicingID)");
        $("#PaymentTermsID").val("@(Model.Order.PaymentTermsID)");
        $("#ScheduleTemplateID").val("@(Model.Order.ScheduleTemplateID)");
        $("#FrameworkAgreementID").val("@(Model.Order.FrameworkAgreementID)");
        $("#CompanyID").val("@(Model.Order.CompanyID)");
        $("#OrderStatusID").val("@(Model.Order.OrderStatusID)");



        $("#buttonAddFee").click(function () {
            var feeCounter = $("#feeCounter").val();
            $("#feeCounter").val(parseInt($("#feeCounter").val(), 10) + 1);
            $.get('@Url.Action("AddFee", "Orders")/?id=' + feeCounter, function (data) {
                $(data).insertBefore("#newfeehere");
            });
        });

        $("#ScheduleTemplateID").change(function () {
            $.get('@Url.Action("ReplaceTemplate","Orders")/?id=' + this.value, function (data) {
                $("#templatesection").replaceWith(data);
            });
        });

        $(function () {
            $("#accordion").accordion({
                collapsible: true,
                autoHeight: false
            });
        });

        $("#ordereditcancel").click(function () {
            $("#editorderdialog").dialog('close');
        });
    });
</script>
